package club.mzywucai.blog.air_portal.pojo.domain;

import club.mzywucai.blog.air_portal.util.DateUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;

/**
 * @author mzywucai
 * @Description
 * @date 2019/5/21
 */
@Data
@Component
@PropertySource({"classpath:config/application.properties"})
public class MakeDir {

    @Value("${server.system.tmp}")
    private String tmpPath;

    @Value("${server.file.base}")
    private String filePath;

    @Value("${server.file.uri}")
    private String fileURI;

    private String todayFilePath;
    private String todayFileURI;

    public String getTodayFilePath() {
        return todayFilePath;
    }

    public String getTodayFileURI() {
        return todayFileURI;
    }


    /**
     * @Description 每日凌晨更新创建目录
     * @author mzywucai
     * @time 2019-05-21
     * @see 
     * @return   void
     * @params  []
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void mkdir() throws Exception {
        File file = new File(filePath);
        File tmp = new File(tmpPath);
        file.mkdirs();
        tmp.mkdirs();

        if (!file.exists()) {
            System.err.println("此计算机系统上无权限创建目录<"+file.getAbsolutePath()+">");
        }
        if (!tmp.exists()) {
            System.err.println("此计算机系统上无权限创建目录<"+tmp.getAbsolutePath()+">");
        }

        Date date = new Date();
        int year = DateUtil.getYear(date);
        int month = DateUtil.getMonth(date);
        int day = DateUtil.getDay(date);
        // 创建当日目录层级
        String todayPathURI = "/"+year+"/"+month+"/"+day+"/";

        // 本地系统中的Path
        todayFilePath = filePath + todayPathURI;

        // Http访问的URI
        todayFileURI = fileURI + todayPathURI;

        file = new File(todayFilePath);

        if (!file.exists()) {
            file.mkdirs();
        }

        if (!file.exists()) {
            System.err.println("此计算机系统上无权限创建目录<"+file.getAbsolutePath()+">");
            throw new Exception("此计算机系统上无权限创建目录");
        }
    }
}
